package com.ruoyi.common.utils.hangxin;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class EtaxUtil {
    private static final Logger log = LoggerFactory.getLogger(EtaxUtil.class);
    private  static  final String API_URL = "https://192.168.98.103:8080/mobile/";
    // 测试
   // private  static  final String API_URL = "https://192.168.98.104:8080/mobile/";
    //  本地
    // dxj private  static  final String API_URL = "http://localhost:9003/mobile/";
    public static String getInvoiceTotal(JSONObject param) {
        String s = "";
        try {
            //s = HttpsUtil.post(API_URL+"/auth/getInvoiceTotal", param.toJSONString());
            s = sendPostData(param,API_URL+"/auth/getInvoiceTotal");
        }catch (Exception e){
            log.info("验证接口错误：{},异常信息：{}",s,e);
        }finally {
            if(CommonUtil.isEmpty(s)){
                s="{\"code\":\"0\",\"msg\":\"通道接口调用失败！\"}";
            }
            return  s;
        }
    }

    private static  String sendPostData(JSONObject data,String url){
        OutputStreamWriter o = null;
        BufferedReader in = null;
        HttpURLConnection conn =null;
        URL realUrl;
        String result = "";
        try {
            realUrl = new URL(url);
            if("https".equalsIgnoreCase(realUrl.getProtocol())){
                SslUtil.ignoreSsl();
            }
            conn = (HttpURLConnection) realUrl.openConnection();
            //设置超时时间
            conn.setConnectTimeout(8000);
            conn.setReadTimeout(8000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","application/json");
            o = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
            System.out.println("传入参数==="+data.toJSONString());
            o.write(data.toJSONString());
            o.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null){
                result = result + line;
            }
            System.out.println(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error("请求通道数据异常：{}",e);
        }finally{
            if(null!=o){
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=in){
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(null!=conn){
                conn.disconnect();
            }
            if(CommonUtil.isEmpty(result)||"".equals(result)){
               result= "{\"code\":\"0\",\"msg\":\"通道接口请求超时！\"}";
            }
            return result;
        }
    }
}
